package it.rap.applications.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class PropertyManager {

	public static final String PROPERTY_PATH = "appsProperties";
	public static final String PROPERTY_APP_NAME = "application";
	public static final String PROPERTY_FILE_NAME = "properties.ini";
	
	private String 		path = null;
	private String 		fileName = null;
	private String 		appName = null;
	private Properties 	properties = new Properties();
	
	public PropertyManager(){
		setParams( null );
		loadProperties();
	}
	
	public PropertyManager( Map<String, String> params ){
		setParams( params );
		loadProperties();
	}
	
	public PropertyManager( Properties params ){
		setParams( this._getPropertiesMap( params ) );
		loadProperties();
	}
	
	public Properties getProperties(){
		return properties;
	}
	
	public Map<String, String> getPropertiesMap(){
		return this._getPropertiesMap( this.properties );
	}
	
	private Map<String, String> _getPropertiesMap( Properties properties){
		Map<String, String> params = new HashMap<String, String>();
		
		Iterator<Entry<Object, Object>> iterator = properties.entrySet().iterator();
		while ( iterator.hasNext() ){
			Entry<Object, Object> item = iterator.next();
			params.put( (String)item.getKey() , (String)item.getValue() );
		}
		return params;
	}
	
	private Properties loadProperties(){
		String filePath = "";
		if ( path.startsWith("/") && path.endsWith("/") ){
			filePath = path ;
		} else  if ( path.startsWith("/") && !path.endsWith("/") ){
			filePath = path + "/" ;
		} else  if ( !path.startsWith("/") && path.endsWith("/") ){
			filePath = "/" + path;
		} else {
			filePath = "/" + path + "/";
		}
		if ( appName != null && !"".equals( appName ) ){
			if ( appName.startsWith("/") && appName.endsWith("/") ){
				filePath = filePath + path ;
			} else  if ( appName.startsWith("/") && !appName.endsWith("/") ){
				filePath = filePath + appName + "/" ;
			} else  if ( !appName.startsWith("/") && appName.endsWith("/") ){
				filePath = filePath + "/" + appName;
			} else {
				filePath = filePath + "/" + appName + "/";
			}
		}
		
		
		filePath = filePath + fileName;
		File file = new File( filePath );
		InputStream inStream;
		try {
			inStream = new FileInputStream(file );
			properties.load( inStream  );
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return properties;
		
	}
	
	private void setParams( Map<String, String> params ){
		setDefault();
		if ( params != null ){
			if ( params.containsKey( PROPERTY_PATH ) ){
				path = params.get( PROPERTY_PATH );
			}
			if ( params.containsKey( PROPERTY_APP_NAME ) ){
				appName = params.get( PROPERTY_APP_NAME );
			}
			if ( params.containsKey( PROPERTY_FILE_NAME ) ){
				fileName = params.get( PROPERTY_FILE_NAME );
			}
		}
	}

	private void setDefault() {
		path = PROPERTY_PATH;
		appName = PROPERTY_APP_NAME;
		fileName = PROPERTY_FILE_NAME;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}
	
}
